﻿var pop ={
    call_iframe : 'call_iframe',
    call_ajax : 'call_ajax',
    call_content : 'call_content',
    call_img : 'call_img',
    call_wmv : 'call_wmv',
    defaultWidth : 500,
    defaultHeight : 500,
    loadImg : 'images/loadImg.gif',
    isIE : (function (){ return ( /*@cc_on!@*/ false) && (parseInt(navigator.userAgent.match(/msie\s(\d+)/i)[1]))})(),
    creatElementsDone : false,
    //初始化
    init : function (){
        if(!document.getElementById || !document.createTextNode){return};
        var all = document.getElementsByTagName('a');
        if(!all) {return};
        for (var i=0,item;item=all[i];i++ ) {
           if ( all[i].rel.indexOf(pop.call_content)!=-1 || all[i].rel.indexOf(pop.call_iframe)!=-1 || all[i].rel.indexOf(pop.call_ajax)!=-1 || all[i].rel.indexOf(pop.call_img)!=-1 || all[i].rel.indexOf(pop.call_wmv)!=-1)
              { pop.addEvent(all[i], 'click', pop.initPop)}
         }
      },
     //初始化pop
     initPop : function (e){
          pop.c = arguments[0];
          pop.obj = ( arguments.length > 2) ? arguments[1] : this;
          if( pop.obj == this ){
             arr01 = (/(&width=)(\d*)/.exec(this.rel)) ? (/(&width=)(\d*)/.exec(this.rel)[2]) : null;
             arr02 = (/(&height=)(\d*)/.exec(this.rel)) ? (/(&height=)(\d*)/.exec(this.rel)[2]) : null;
             pop.stopDefault(e);
          }
          pop.width = arguments[2] || arr01 || pop.defaultWidth;
          pop.height = arguments[3] || arr02 || pop.defaultHeight;
          pop.caption = arguments[4] || this.title || false; 
          pop.creatElement();
          pop.removePop(); 
      },
      //创建pop
      creatElement : function (){
          //建立元素
		   var creatDIV = document.createElement("div");
          if(!pop.creatElementsDone){
             pop.pM = creatDIV.cloneNode(true);
             pop.pM.id = 'pop_mask';
             pop.pId = creatDIV.cloneNode(true);
             pop.pId.id = 'pop_wrapper'; 
             pop.pW = creatDIV.cloneNode(true);
             pop.pW.id = 'pop_container' ; 
             pop.close = document.createElement('span');
             pop.close.title = '关闭弹出窗口';
             pop.close.innerHTML = pop.closetxt;
             pop.content = creatDIV.cloneNode(true);
             pop.content.id = 'pop_content';
             //添加元素
             pop.pId.appendChild( pop.pW );
			 pop.h3 = document.createElement('h3');
			 pop.pW.appendChild( pop.h3 );
             pop.pW.appendChild( pop.close );
             pop.pW.appendChild( pop.content );
             document.body.appendChild(pop.pId);
             document.body.appendChild(pop.pM);
			 if(pop.isIE){
			    pop.ieFix();
			 }
			 pop.creatElementsDone = true;
          };
		  pop.pId.style.display = '';
		  pop.pM.style.display = '';
          if( pop.caption ){
			   pop.h3.style.display = 'block';
			   pop.h3.innerHTML = pop.caption;
           }
		   else{
			   pop.h3.innerHTML = '';
			   pop.h3.style.display = 'none'; 
		   }
          //设置style
          pop.content.style.height = pop.height + 'px';
          pop.height = parseInt(pop.height) + (pop.caption ? parseInt(pop.h3.offsetHeight) : 0);
          pop.show();
       },
       show : function (){
          pop.resetCSS( pop.pId, { width :'0', height :'0', display :'none' });
		  pop.pW.style.display = 'none';
		  function animation(){
			pop.pId.style.display = 'block';
			var time = new Date().getTime();
			var elapsed = null;
			var timer = setInterval(function (){
				var times = new Date().getTime();
				if(times < (time+1000)){
				   elapsed = times - time;
                   setCurrentFrame();
				}
				else{
                  endStatus();
				  clearInterval(timer)
				}
			},10);
			function setCurrentFrame(){
			   pop.pId.style.width = pop.easeOut(elapsed,22,parseInt(pop.width)+22,1000) + 'px';
			   pop.pId.style.height = pop.easeOut(elapsed,22,parseInt(pop.height),1000) + 'px';
			   if(pop.isIE != 6){ pop.pId.style.marginTop = '-' + (parseInt(pop.pId.style.height) / 2) + 'px' };
			   pop.pId.style.marginLeft =  '-' + (parseInt(pop.pId.style.width) / 2) + 'px';
			};

			function endStatus(){
			  pop.pId.style.width = parseInt(pop.width)+22 + 'px';
			  pop.pId.style.height = parseInt(pop.height) + 'px';
			  if(!(pop.isIE &&　pop.isIE < 7)){ pop.pId.style.marginTop = '-' + (parseInt(pop.pId.style.height) / 2) + 'px' };
			  pop.pId.style.marginLeft =  '-' + (parseInt(pop.pId.style.width) / 2) + 'px';
			  pop.pW.style.display = "block"; 
			  pop.typePop();          
			};
		  }
          setTimeout( animation , 500 );
       },
      // 判断pop类型
      typePop : function (){
          var link = (typeof(pop.obj) == 'string') ? pop.obj : pop.obj.href;
          function type(arg){
             return ((!!pop.obj.rel ) ? pop.obj.rel.indexOf(arg)!=-1 : false) || ( pop.c == arg);
          }
          //ajax调用
          if ( type(pop.call_ajax) ){
             pop.content.style.backgroundImage = 'url(' + pop.loadImg + ')';
             function setHeader(req) {
                  pop.content.style.backgroundImage = 'none';
                  pop.content.innerHTML = req.responseText;
             };
             pop.ajax.sendRequest( link + '?' + Math.random(),setHeader );
           }
           //wmv调用
           else if ( type(pop.call_wmv) ){
              pop.content.style.backgroundImage = 'url(' + pop.loadImg + ')';
			  pop.content.innerHTML = '<object type="video/x-ms-wmv" id="object_wmv" width=' + pop.width + ' height='+ (pop.height-30) +'>'+
					'<param name="src" value='+link+' />'+
					'<param name="BufferingTime" value="5" />'+
					'<param name="ShowStatusBar" value="1" >'+
					'<param name="autostart" value="true" />'+
					'<param name="controller" value="true" />'+
					'<param name="fullScreen" value="0">'+
				  '</object>';
           }
           //iframe调用
           else if ( type(pop.call_iframe) ){
               pop.content.style.backgroundImage = 'url(' + pop.loadImg + ')';
               pop.content.innerHTML = "<iframe frameborder=0 id='ifr' src=" + link + "?"+Math.round(Math.random()*1000)+" height=" +parseInt(pop.height)+" name=frname"+Math.round(Math.random()*1000)+"></iframe>"; 
               var o = document.getElementById("ifr");
               o.style.display = 'none';
               o.onload = o.onreadystatechange = function (){
                 this.style.display = 'block';
                 pop.content.style.backgroundImage = 'none';
               }
           }
           //inner-html调用
           else if ( type(pop.call_content) ){
               var section = link.match(/#(.*)/)[1];
               section = document.getElementById(section);
               pop.content.innerHTML = section.innerHTML;
            }
           //image调用
           else if (type(pop.call_img)){
               var img = document.createElement("img");
               pop.content.style.backgroundImage = 'url(' + pop.loadImg + ')';
               img.onload = function (){
                  pop.content.style.backgroundImage = 'none';
                  pop.content.appendChild(img);
               }
               img.src = link;
            }
      },
       // 移除窗口
       removePop : function (){
           pop.close.onclick = pop.hide;
       },
	   hide : function (){
		  function animation(){
			var time = new Date().getTime();
			var elapsed = null;
			var timer = setInterval(function (){
				var times = new Date().getTime();
				if(times < (time+500)){
				   elapsed = times - time;
                   pop.setOpacity(pop.pId,pop.easeOut(elapsed,100,-100,500))
				}
				else{
				   pop.content.innerHTML = '';
				   pop.content.style.backgroundImage = 'none';
				   pop.pM.style.display = 'none';
				   pop.pId.style.display = 'none';
				   pop.setOpacity(pop.pId,100);
				   if(pop.pId.filters){ pop.pId.style.filter = '' };
				   if(pop.isIE == 6) {pop.Ifr.style.display = 'none'};
				   clearInterval(timer);
				}
			},20);
		  }
          animation();
       },
       addEvent : function ( obj, type, fn ) {
           if (obj.addEventListener)
               obj.addEventListener(type, fn, false);
           else if (obj.attachEvent) 
               obj.attachEvent('on' + type, function() { return fn.apply(obj, new Array(window.event));});
       },
       stopDefault : function ( e ) {
           if ( e && e.preventDefault )
               e.preventDefault();
           else
               window.event.returnValue = false;
           return false;
       },
       setOpacity : function( elem, level ) {
             if ( elem.filters )
                 elem.style.filter = 'alpha(opacity=' + level + ')';
             else
                 elem.style.opacity = level / 100;
       },
       easeOut : function(t,b,c,d){
            if ((t/=d/2) < 1) return c/2*t*t*t + b;
            return c/2*((t-=2)*t*t + 2) + b;
       },
       ieFix : function (){
		  var dd = (document.compatMode && document.compatMode=='CSS1Compat') ? document.documentElement : document.body ;
		  pop.pM.style.height = dd.scrollHeight > dd.offsetHeight ? dd.scrollHeight : dd.offsetHeight + 'px';
          if(pop.isIE <= 6)
          {
            var sh = document.body.offsetHeight;
            pop.Ifr = document.createElement('iframe');
            document.body.appendChild(pop.Ifr);
            pop.resetCSS( pop.Ifr, { position :'absolute', zIndex :'1', width :'100%', height : sh+'px', left:'0', top: '0',
            filter :'alpha(opacity=0)',border:'none' })
          }
       },
       resetCSS : function ( elem, prop ) {
           for ( var i in prop ) { elem.style[ i ] = prop[i]; }
       },
       ajax : {
           sendRequest : function ( url,callback,postData){
              var req = pop.ajax.createXMLHTTPObject();
              if (!req) return;
              var method = (postData) ? "POST" : "GET";
              req.open(method,url,true);
              req.setRequestHeader('User-Agent','XMLHTTP/1.0');
              if (postData)
               req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
              req.onreadystatechange = function () {
               if (req.readyState != 4) return;
                 callback(req);
              }
              if (req.readyState == 4) return;
              req.send(postData);           
           },
           XMLHttpFactories : function (){
             return [
              function () {return new XMLHttpRequest()},
              function () {return new ActiveXObject("Msxml2.XMLHTTP")},
              function () {return new ActiveXObject("Msxml3.XMLHTTP")},
              function () {return new ActiveXObject("Microsoft.XMLHTTP")}
             ];
           },
           createXMLHTTPObject : function (){
               var xmlhttp = false;
               var factories = pop.ajax.XMLHttpFactories();
               for (var i=0;i<factories.length;i++) {
                try { xmlhttp = factories[i](); }
                catch (e) { continue;}
                break;
               }
               return xmlhttp;           
           }
       } 
}

$(function (){
   pop.init();
})